package com.yami.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.dto.MeetingDto;
import com.yami.shop.bean.dto.ProductShowDto;
import com.yami.shop.bean.model.Meeting;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.MeetingMapper;
import com.yami.shop.service.MeetingService;
import com.yami.shop.service.ProductService;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * @author lgh on 2019/03/27.
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private SysConfigService sysConfigService;

    @Override
    @Cacheable(cacheNames = "MeetingDtoList", key = "#shopId")
    public List<Meeting> getMeetingDtoList(Long shopId) {
        return  meetingMapper.selectList(new LambdaQueryWrapper<Meeting>().eq(Meeting::getShopId, shopId));
    }

    @Override
    @Cacheable(cacheNames = "MeetingDto", key = "#meetingId")
    public MeetingDto getMeetingDtoByShopIdAndMeetingId(Long shopId, Long meetingId) {
        Meeting meeting = meetingMapper.selectOne(new LambdaQueryWrapper<Meeting>().eq(Meeting::getMeetingId, meetingId).eq(Meeting::getShopId, shopId));
        MeetingDto meetingDto = BeanUtil.copyProperties(meeting, MeetingDto.class);
        // 获取商品信息
        Product productById = productService.getProductById(meetingDto.getProdId());
        meetingDto.setProductShowDto(BeanUtil.copyProperties(productById, ProductShowDto.class));
        return meetingDto;
    }

    @Override
    @CacheEvict(cacheNames = "MeetingDto", key = "#meetingId")
    public void removeMeetingDtoCacheByMeetingId(Long meetingId) {

    }

    @Override
    @CacheEvict(cacheNames = "MeetingDtoList", key = "#shopId")
    public void removeMeetingListCacheByShopId(Long shopId) {

    }

    @Override
    public IPage<Meeting> page(PageParam<Meeting> page, Meeting meeting) {
        LambdaQueryWrapper<Meeting> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Meeting::getShopId, meeting.getShopId());
        queryWrapper.orderByDesc(Meeting::getCreateTime);
        return meetingMapper.selectPage(page,queryWrapper);
    }

    @Override
    public int updateMeeting(Meeting meeting) {
        return meetingMapper.updateByPrimaryKeySelective(meeting);
    }

    @Override
    public int deleteMeeting(Long[] ids) {
        int resul = 0;
        for (Long id : ids){
            Meeting meeting = new Meeting();
            meeting.setMeetingId(id);
            meeting.setMeetingStatus(0);
            meeting.setUpdateTime(new Date());
            resul = meetingMapper.updateById(meeting);
        }
        return resul;
    }

    @Override
    public void addMeeting(Meeting meeting) {
        meetingMapper.insertSelective(meeting);
        // 生成二维码地址
        String value = sysConfigService.getValue("meetingUrl");
        // 会议分享链接
        meeting.setMeetingPublicUrl(value+"?meetingId="+meeting.getMeetingId());
        // 核销记录链接
        meeting.setMeetingRecordsUrl(value+"pages/record/record?id="+meeting.getMeetingId());
        meetingMapper.updateByPrimaryKey(meeting);
        removeMeetingListCacheByShopId(meeting.getShopId());
    }
}
